package com.situ.company.web220111.department.dao.impl;

import com.situ.company.web220111.department.dao.IDepartmentDao;
import com.situ.company.web220111.department.model.DepartmentModel;
import com.situ.company.web220111.util.FmtEmpty;
import com.situ.company.web220111.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Department1DaoImpl implements IDepartmentDao {

    private static String table = "department";
    private static String cols = "code,name,tel";
    private Connection conn;
    private PreparedStatement ps;

    @Override
    public String insert(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("insert into ");
        sql.append(table).append('(').append(cols).append(")values(?,?,?)");
        try {
            conn = JDBCUtil.getConnection();
            ps = conn.prepareStatement(sql.toString());
            ps.setString(1, model.getCode());
            ps.setString(2, model.getName());
            ps.setString(3, model.getTel());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn, ps);
        }
        return null;
    }

    @Override
    public String delete(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("delete from ");
        sql.append(table).append("where code = ?");
        try {
            conn = JDBCUtil.getConnection();
            ps = conn.prepareStatement(sql.toString());
            ps.setString(1, model.getCode());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn, ps);
        }
        return null;
    }

    @Override
    public String updateAll(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("update ");
        sql.append(table).append("set name = ?,tel  = ? where code = ?");
        try {
            conn = JDBCUtil.getConnection();
            ps = conn.prepareStatement(sql.toString());
            ps.setString(1, model.getName());
            ps.setString(2, model.getTel());
            ps.setString(3, model.getCode());
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn, ps);
        }
        return null;
    }

    @Override
    public String updateActive(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("update ");
        List<Object> vals = appendSet(model, sql);
        try {
            conn = JDBCUtil.getConnection();
            ps = conn.prepareStatement(sql.toString());
            for (int i = 0; i < vals.size(); i++)
                ps.setObject(i + 1, vals.get(i));
            return ps.executeUpdate() + "";
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn, ps);
        }
        return null;
    }

    private List<Object> appendSet(DepartmentModel model, StringBuffer sql) {
        List<Object> vals = new ArrayList<>();
        sql.append(table).append("set id = id ");
        String name = model.getName();
        if (name != null) { // name == null 没有对象|| name.trim().isEmpty() 有对象但是没有数据
            sql.append(", name = ? ");
            vals.add(name);
        }
        String tel = model.getTel();
        if (tel != null) { // name == null 没有对象|| name.trim().isEmpty() 有对象但是没有数据
            sql.append(",tel = ? ");
            vals.add(tel);
        }
        sql.append("where code = ?");
        vals.add(model.getCode());
        return vals;
    }

    @Override
    public List<DepartmentModel> selectList(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("select id,").append(cols);
        sql.append(" from ").append(table);
        List vals = appendWhere(model, sql);
        List<DepartmentModel> list = new ArrayList<>();
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnection();
            ps = conn.prepareStatement(sql.toString());
            for (int i = 0; i < vals.size(); i++)
                ps.setObject(i + 1, vals.get(i));
            rs = ps.executeQuery();
            DepartmentModel model1;
            while (rs.next()) {
                model1 = new DepartmentModel();
                model1.setId(rs.getInt("id"));
                model1.setCode(rs.getString("code"));
                model1.setName(rs.getString("name"));
                model1.setTel(rs.getString("tel"));
                list.add(model1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn, ps, rs);
        }
        return list;
    }


    private List<Object> appendWhere(DepartmentModel model, StringBuffer sql) {
        sql.append("where 1=1 ");
        List<Object> vals = new ArrayList<>();
        String code = model.getCode();
        if (FmtEmpty.isEmpty(code)) {
            sql.append(",code = ? ");
            vals.add(code);
        }
        String name = model.getName();
        if (FmtEmpty.isEmpty(name)) {
            sql.append(",name = ? ");
            vals.add(name);
        }
        String tel = model.getTel();
        if (FmtEmpty.isEmpty(tel)) {
            sql.append(",tel = ? ");
            vals.add(tel);
        }
        return vals;
    }

    @Override
    public DepartmentModel selectModel(DepartmentModel model) {
        StringBuffer sql = new StringBuffer("select id,").append(cols);
        sql.append(" from ").append(table).append(" where code = ? ");
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnection();
            ps = conn.prepareStatement(sql.toString());
            ps.setObject(1, model.getCode());
            rs = ps.executeQuery();
            if (rs.next()) {
                DepartmentModel model1 = new DepartmentModel();
                model1.setId(rs.getInt("id"));
                model1.setCode(rs.getString("code"));
                model1.setName(rs.getString("name"));
                model1.setTel(rs.getString("tel"));
                return model1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.close(conn, ps, rs);
        }
        return null;
    }

    @Override
    public Integer selectCount(DepartmentModel model) {
        return null;
    }
}
